//
//  BaseWhiteBoxPC.m
//  BaseCode
//
//  Created by 曹连华 on 2020/3/24.
//

#import "BaseWhiteBoxPC.h"
#import <CoreCode/CoreCode.h>
#import <Masonry/Masonry.h>
#import "UIViewController+base.h"

@interface BaseWhiteBoxPC ()<BasePCDelegate>

@end

@implementation BaseWhiteBoxPC

- (UIView *)bgContentView {
    if (_bgContentView == nil) {
        _bgContentView = [[UIView alloc]init];
        _bgContentView.backgroundColor = [self getBgContentViewColor];
        _bgContentView.layer.cornerRadius = 3;
        
    }
    return _bgContentView;
}

- (UIView *)bgBottomView {
    if(!_bgBottomView) {
        _bgBottomView = [[UIView alloc]init];
        _bgBottomView.backgroundColor = [UIColor whiteColor];
    }
    return _bgBottomView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self p__initUI];
    [self p__setupData];
    [self p__addObserve];
    self.pcDelegate  = self;
}
- (void)p__initUI {
    [self.view addSubview:self.bgContentView];
    [self.bgContentView addSubview:self.bgBottomView];
    [self.bgContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.width.mas_equalTo([self getBgContentViewWidth]);
        make.height.mas_equalTo([self getBgContentViewHeight]);
        make.top.mas_equalTo([UIScreen height]);
    }];
    
    [self.bgBottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(UIScreen.screenType == PBScreenTypeNormal?0:-34);
        make.leading.trailing.mas_equalTo(0);
        make.height.mas_equalTo([self getBgBottomViewHeight]);
    }];
}
- (void)p__setupData {
    
}
- (void)p__addObserve {
    [self observeNotification:UIKeyboardWillShowNotification];
    [self observeNotification:UIKeyboardWillHideNotification];
}
#pragma mark - BasePCDelegate
- (void)willPresentPC:(BasePC *)aPresentationController {
    [self postNotification:PB_BASEPC_WILLPRESENTPC];
}

- (void)didDismissPC:(BasePC *)aPresentationController {
    [self postNotification:PB_BASEPC_DIDDISMISSPC];
}

#pragma mark - BasePC
- (void)presentViewControllerAnimated:(BOOL)animated completion:(void (^)(void))completion {
    [super presentViewControllerAnimated:animated completion:completion];
    [self.bgContentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo([UIScreen height]-[self getBgContentViewHeight]);
    }];
    
    [UIView animateWithDuration:self.duration delay:0.f usingSpringWithDamping:.8f initialSpringVelocity:0.f options:0 animations:^{
        [self.view layoutIfNeeded];
    }                completion:nil];
}

- (void)dismissViewControllerAnimated:(BOOL)animated completion:(void (^)(void))completion {
    [super dismissViewControllerAnimated:animated completion:completion];
    [self.bgContentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo([UIScreen height]);
    }];
    
    [UIView animateWithDuration:self.duration delay:0.f usingSpringWithDamping:.8f initialSpringVelocity:0.f options:0 animations:^{
        [self.view layoutIfNeeded];
    } completion:nil];
    
}

- (CGFloat)getBgContentViewHeight {
    return [UIScreen height] * BaseWhiteBoxPCHeightRatio;
}

- (CGFloat)getBgContentViewWidth {
    return [UIScreen width];
}

- (CGFloat)getBgBottomViewHeight {
    return 44;
}

- (UIColor *)getBgContentViewColor {
    return [UIColor whiteColor];
}

- (void)dealloc {
    [self unobserveNotification:UIKeyboardWillShowNotification];
    [self unobserveNotification:UIKeyboardWillHideNotification];
}

@end
